package com.hehui.module.customer.service.impl;

import com.hehui.common.core.commom.TreeSelect;
import com.hehui.common.utils.StringUtils;
import com.hehui.module.customer.dao.CustomLocaleMapper;
import com.hehui.module.customer.entity.CustomLocale;
import com.hehui.module.customer.service.ICustomLocaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CustomLocaleServiceImpl implements ICustomLocaleService {

    @Autowired
    private CustomLocaleMapper customLocaleMapper;

    /*根据自定义区域ID查询*/
    @Override
    public CustomLocale selectCustomLocaleById(Long localeId) {
        return customLocaleMapper.selectCustomLocaleById(localeId);
    }

    /*添加节点信息*/
    @Override
    public int insertCustomLocale(CustomLocale customLocale) {
        //查询父节点信息
        CustomLocale parentInfo = customLocaleMapper.selectCustomLocaleById(customLocale.getParentId());
        //添加祖籍列表
        customLocale.setAncestors(parentInfo.getAncestors() + "," + customLocale.getParentId());

        if (customLocale.getIsdef().equals("0")) {
            //如果新添加的节点被设置为默认，那么要修改之前被设置默认的节点为非默认
            customLocaleMapper.setUndefault(customLocale.getMerchantId());
        }
        //保存新节点
        return customLocaleMapper.insertCustomLocale(customLocale);
    }

    /*修改节点信息*/
    @Override
    public int updateCustomLocale(CustomLocale customLocale) {
        //查询父节点信息
        CustomLocale parentInfo = customLocaleMapper.selectCustomLocaleById(customLocale.getParentId());
        //修改前的信息
        CustomLocale oldInfo = customLocaleMapper.selectCustomLocaleById(customLocale.getLocaleId());
        if (StringUtils.isNotNull(parentInfo) && StringUtils.isNotNull(oldInfo)) {
            //定义自己的祖籍IDS
            String usAncestors = parentInfo.getAncestors() + "," + parentInfo.getLocaleId();
            //修改前的祖籍ID
            String oldAncestors = oldInfo.getAncestors();
            customLocale.setAncestors(usAncestors);
            // 如果我自己还有子节点，我还要更新我子节点对应的祖籍ID
            updateCustomLocaleChildren(customLocale.getLocaleId(), usAncestors, oldAncestors);
        }
        if (customLocale.getIsdef().equals("0")) {
            //如果该节点被设置为默认，那么要修改之前被设置默认的节点为非默认
            customLocaleMapper.setUndefault(customLocale.getMerchantId());
        }
        //更新
        return customLocaleMapper.updateCustomLocale(customLocale);
    }

    //删除节点
    @Override
    public int deleteCustomLocaleById(Long localeId) {
        return customLocaleMapper.deleteCustomLocaleById(localeId);
    }

    //根据 ID查询是否存在子节点  如果有子节点 return true
    @Override
    public boolean hasChildByCustomLocaleById(Long localeId, Long merchantId) {
        int i = customLocaleMapper.hasChildByCustomLocaleById(localeId, merchantId);
        return i == 0 ? false : true;
    }

    //根据ID查询 该节点下有没有用户 如果有用户 return true
    @Override
    public boolean checkCustomLocaleExistCustomer(Long localeId, Long merchantId) {
        int i = customLocaleMapper.checkCustomLocaleExistCustomer(localeId, merchantId);
        return i == 0 ? false : true;
    }

    //查询归属地全部数据
    @Override
    public List<CustomLocale> selectAllCustomLocale(CustomLocale customLocale) {


        return customLocaleMapper.selectAllCustomLocale(customLocale);
    }

    //构建前端所需的树结构
    @Override
    public List<CustomLocale> buildCustomLocaleTree(List<CustomLocale> customLocales) {
        List<CustomLocale> returnList = new ArrayList<>();
        //保存传入的节点ID
        List<Long> tempList = new ArrayList<>();
        for (CustomLocale customLocale : customLocales) {
            tempList.add(customLocale.getLocaleId());
        }
        for (Iterator<CustomLocale> iterator = customLocales.iterator(); iterator.hasNext(); ) {
            CustomLocale locale = (CustomLocale) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(locale.getParentId())) {
                recursionFn(customLocales, locale);
                returnList.add(locale);
            }
        }
        if (returnList.isEmpty()) {
            returnList = customLocales;
        }
        return returnList;
    }

    //构建前端所需要的下拉结构
    @Override
    public List<TreeSelect> buildCustomLocaleTreeSelect(List<CustomLocale> customLocales) {
        List<CustomLocale> customLocaleTree = buildCustomLocaleTree(customLocales);
        return customLocaleTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    //根据ID查询所有子节点 ID
    @Override
    public List<CustomLocale> selectChildrenCustomLocaleById(Long localeId) {
        return customLocaleMapper.selectChildrenCustomLocaleById(localeId);
    }


    //根据节点名查询节点是否存在   如果集合有数据返回 true
    @Override
    public boolean checkCustomLocaleNameUnique(String name, Long merchantId) {

        return customLocaleMapper.checkCustomLocaleNameUnique(name, merchantId).size() > 0 ? true : false;
    }
    //根据节点编号查询节点是否存在   如果集合有数据返回 true
    @Override
    public boolean checkCustomLocaleNumUnique(String num, Long merchantId) {
        return customLocaleMapper.checkCustomLocaleNumUnique(num, merchantId).size() > 0 ? true : false;
    }

    //生成新的编号
    @Override
    public String createNewNum(Long merchantId) {
        String lastNum = customLocaleMapper.getMaxNum(merchantId);
        if (lastNum == null || lastNum.equals("")) {
            lastNum = "1000";
        }
        return Integer.parseInt(lastNum) + 1 + "";
    }



    //修改子元素关系
    public void updateCustomLocaleChildren(Long deptId, String newAncestors, String oldAncestors) {
        //拿到所有子节点信息
        List<CustomLocale> children = customLocaleMapper.selectChildrenCustomLocaleById(deptId);
        for (CustomLocale child : children) {
            //设置要修改的信息
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        //修改信息
        if (children.size() > 0) {
            customLocaleMapper.updateCustomLocaleChildren(children);
        }

    }

    //递归
    private void recursionFn(List<CustomLocale> list, CustomLocale t) {
        //得到子节点列表
        List<CustomLocale> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CustomLocale tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }

    }

    /**
     * 得到子节点列表
     */
    private List<CustomLocale> getChildList(List<CustomLocale> list, CustomLocale t) {
        List<CustomLocale> tlist = new ArrayList<>();
        Iterator<CustomLocale> it = list.iterator();
        while (it.hasNext()) {
            CustomLocale n = (CustomLocale) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getLocaleId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CustomLocale> list, CustomLocale t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
